﻿namespace Yom.Common
{
    using System;
    using System.Text.RegularExpressions;
    using System.Web;

    /// <summary>
    /// 成员方法操作类
    /// 
    /// 创建: 邱柯皓 2009/02/04 19:00
    /// </summary>
    public static class Fetch
    {
        #region 基本方法
        /// <summary>
        /// 转换字符串为MD5加密字符串
        /// 
        /// 创建: 邱柯皓 2009/02/04 19:01
        /// </summary>
        /// <param name="str">要转换的字符串</param>
        /// <returns>返回MD5加密字符串</returns>
        public static string ToMd5(this string str)
        {
            return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5").ToLower();
        }

        /// <summary>
        /// 截取指定长度字符串, 并设置超出部分显示内容
        /// 
        /// 创建: 邱柯皓 2009/02/04 19:04
        /// </summary>
        /// <param name="str">要截取的字符串</param>
        /// <param name="length">要截取的长度</param>
        /// <param name="more">超出部分显示内容</param>
        /// <returns>返回截取后的字符串</returns>
        public static string SubStr(this string str, int length, string more)
        {
            if (str.Length <= length)
                return str;

            return str.Substring(0, length) + more;
        }

        /// <summary>
        /// 截取指定长度字符串
        /// 
        /// 创建: 邱柯皓 2009/02/04 19:06
        /// </summary>
        /// <param name="str">要截取的字符串</param>
        /// <param name="length">要截取的长度</param>
        /// <returns>返回截取后的字符串</returns>
        public static string SubStr(this string str, int length)
        {
            if (str.Length <= length)
                return str;

            return str.Substring(0, length);
        }

        /// <summary>
        /// 获取HTML编码字符串
        /// 
        /// 创建: 邱柯皓 2009/02/04 19:17
        /// </summary>
        /// <param name="str">要编码的字符串</param>
        /// <returns>返回编码后的字符串</returns>
        public static string HtmlEncode(this string str)
        {
            if (!string.IsNullOrEmpty(str))
                str = HttpUtility.HtmlEncode(str);

            return str;
        }

        /// <summary>
        /// 获取HTML解码字符串
        /// 
        /// 创建: 邱柯皓 2009/02/04 19:19
        /// </summary>
        /// <param name="str">要解码的字符串</param>
        /// <returns>返回解码后的字符串</returns>
        public static string HtmlDeCode(this string str)
        {
            return HttpContext.Current.Server.HtmlDecode(str);
        }

        /// <summary>
        /// 获取SQL过滤字符串
        /// 
        /// 创建: 邱柯皓 2009/02/04 19:20
        /// </summary>
        /// <param name="str">要过滤的字符串</param>
        /// <returns>返回过滤后的字符串</returns>
        public static string SQLFilter(this string str)
        {
            return str.Replace("'", "''");
        }

        /// <summary>
        /// 过滤危险字符
        /// 
        /// 创建: 邱柯皓 2009/02/04 19:22
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string HtmlFilter(this string str)
        {
            str = Regex.Replace(str, @"<\s*(\/?)\s*(script|i?frame|style|html|body|title|link|meta|object|\?|\%)([^>]*?)>", string.Empty, RegexOptions.IgnoreCase);
            str = Regex.Replace(str, @"(<[^>]*)on[a-zA-Z]+\s*=([^>]*>)", "$1>", RegexOptions.IgnoreCase);

            return str;
        }

        /// <summary>
        /// 获取URL编码字符串
        /// 
        /// 创建: 邱柯皓 2009/02/04 19:34
        /// </summary>
        /// <param name="str">要编码的字符串</param>
        /// <returns>返回编码后的字符串</returns>
        public static string UrlEncode(this string str)
        {
            return HttpContext.Current.Server.UrlEncode(str);
        }

        /// <summary>
        /// 获取URL解码字符串
        /// 
        /// 创建: 邱柯皓 2009/02/04 19:35
        /// </summary>
        /// <param name="str">要解码的字符串</param>
        /// <returns>返回解码后的字符串</returns>
        public static string UrlDeCode(this string str)
        {
            return HttpContext.Current.Server.UrlDecode(str);
        }
        #endregion

        #region 数据验证
        /// <summary>
        /// 是否为数字字符串
        /// 
        /// 创建: 邱柯皓 2009/02/04 19:27
        /// </summary>
        /// <param name="str">要验证的字符串</param>
        /// <returns>返回验证结果</returns>
        public static bool IsNumber(this string str)
        {
            Regex regex = new Regex("^[0-9]+$");
            Match m = regex.Match(str);

            return m.Success;
        }

        /// <summary>
        /// 是否为邮件地址
        /// 
        /// 创建: 邱柯皓 2009/02/04 19:29
        /// </summary>
        /// <param name="str">要验证的字符串</param>
        /// <returns>返回验证结果</returns>
        public static bool IsEmail(this string str)
        {
            Regex regex = new Regex("^[\\w-]+@[\\w-\\.]+\\.(com|net|org|gov|edu|cn|tw|hk|la|name|mil|tv|biz|info|asia)$");
            Match m = regex.Match(str);

            return m.Success;
        }
        #endregion

        #region 数据转换
        /// <summary>
        /// 转换为Int类型数据
        /// 
        /// 创建: 邱柯皓 2009/02/04 19:09
        /// </summary>
        /// <param name="obj">要转换的数据</param>
        /// <returns>返回转换后的数据</returns>
        public static int ToInt(this object obj)
        {
            if (!string.IsNullOrEmpty(obj.ToString()))
                return Convert.ToInt32(obj);

            return 0;
        }

        /// <summary>
        /// 转换为Long类型数据
        /// 
        /// 创建: 邱柯皓 2009/02/04 19:11
        /// </summary>
        /// <param name="obj">要转换的数据</param>
        /// <returns>返回转换后的数据</returns>
        public static long ToLong(this object obj)
        {
            if (!string.IsNullOrEmpty(obj.ToString()))
                return Convert.ToInt64(obj.ToString());

            return 0;
        }

        /// <summary>
        /// 转换为DateTime类型数据
        /// 
        /// 创建: 邱柯皓 2009/02/04 19:12
        /// </summary>
        /// <param name="obj">要转换的数据</param>
        /// <returns>返回转换后的数据</returns>
        public static DateTime ToDateTime(this object obj)
        {
            try
            {
                return Convert.ToDateTime(obj.ToString());
            }
            catch
            {
                return DateTime.MinValue;
            }
        }

        /// <summary>
        /// 转换为Bool类型数据
        /// 
        /// 创建: 邱柯皓 2009/02/04 19:14
        /// </summary>
        /// <param name="obj">要转换的数据</param>
        /// <returns>返回转换后的数据</returns>
        public static bool ToBool(this object obj)
        {
            if (obj.ToString() == "1" || obj.ToString().ToLower() == "true")
                return true;

            return false;
        }
        #endregion
    }
}